package com.abigdreamer.im.server.cmd.commandAction;

import com.abigdreamer.im.server.cmd.command.Command;

/**
 * 
 * 所以Action的父类，以后各个命令的Action必须继承于它
 */
public abstract class CommandAction {

	private String nextAction;//本Action责任链的后一个Action
	private String operateParam;
	
	public final String handleMessage(Command command){
		String result="";
		//判断是否是自己处理的参数
		//1):==0：头节点没参数就是0，那么它就得执行
		//2):或者命令后的参数是和本Action相吻合的
		if("help".equals(command.getCommandName())){
			result=this.echo(command);;
		}else{
			if(0==command.getParam().size()||command.getParam().contains(this.getOperateParam())){
				result = this.echo(command);
			}else{
				if(0==this.nextAction.length()||null==this.nextAction){
					result=command.toString()+"命令无法进行,请检查格式或确定系统是否支持!";
				}else{
					CommandAction  action=ActionFactory.getInstance().getCommandActionByName(nextAction);
					result=action.handleMessage(command);
				}
			}
		}
		return result;
	}


	//设置下个Action
	public void setNextAction(String nextAction) {
		this.nextAction = nextAction;
	}
	
	//每个处理者都要处理一个后缀参数
	protected  String getOperateParam(){
		return operateParam;
	};
	
	//每个处理者都必须实现处理任务
	protected abstract String echo(Command command);


	public void setOperateParam(String operateParam) {
		this.operateParam = operateParam;
	}
}
